package com.k.base.jdk.proxy.demo2.frm;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class ProxyUtils {
    public static Object proxyInstance(ClassLoader loader,
                                       Class<?>[] interfaces,
                                       InvocationHandler h){
        return Proxy.newProxyInstance(loader,interfaces,h);
    }

    public static <T extends ProxyAction> Object proxyInstance(ClassLoader loader,
                                       Class<?>[] interfaces,Object interfaceImplObj,T action){
        InterfaceImplObjectInvocationHandler handler = new InterfaceImplObjectInvocationHandler(interfaceImplObj, action);
        return proxyInstance(loader,interfaces,handler);
    }

    /**
     *
     * @param loader classLoader
     * @param interfaces interfaces  实现类实现的所有接口, 就可以使用接口中的方法
     * @param interfaceImplObj interfaceImplObject 实现类对象
     * @param action aop action
     * @param <E> interface type
     * @param <T> action type
     * @return proxy object com.sun.proxy.$Proxy0
     */
    public static  <E,T extends ProxyAction> Object proxyInstance1(ClassLoader loader,
                                                               Class<E>[] interfaces,E interfaceImplObj,T action){
        InterfaceImplObjectInvocationHandler handler = new InterfaceImplObjectInvocationHandler(interfaceImplObj, action);
        return proxyInstance(loader,interfaces,handler);
    }
}
